//
//  UserInfomationVM.swift
//  LeTu
//
//  Created by gy on 2023/12/11.
//

import Foundation

class UserInfomationVM {
    
    private(set) var items = [ListSectionItem]()
    
    // 头像
    var picData: Data?
    // 临时url
    private var tempHeadUrl: String?
    
    init() {
        updateItems()
    }
    
    /// 更新页面数据
    func updateItems() {
        
        items = [ListSectionItem(title: "头像".localized, img: kUserManager.userModel.avatar),
                 ListSectionItem(title: "昵称".localized, content: kUserManager.userModel.nickname)]
        
    }
    
    /// 修改昵称
    func setInvite(params: [String: Any], completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .modefyNickname(params: params)) { json, jsonArr, code in
            MYLog("修改昵称：",json)
            
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
                
    }
    
    // MARK: - 上传头像
    func updateAvatar(completeHandle: @escaping NetworkRequestHandle) {
        
        let queue = DispatchQueue(label: "CommitFeedbackQueue", attributes: .concurrent)
        let sema = DispatchSemaphore(value: 1)
        
        // 上传文件
        queue.async { [weak self] in

            sema.wait()

            guard let strongSelf = self, let data = strongSelf.picData, data.count > 0 else {
                sema.signal()
                return
            }
            
            FeedbackVM.updateFile(data) { [weak self] dialog in
                guard let strongSelf = self, let url = dialog.data as? String, url.count > 0 else {
                    sema.signal()
                    var dialog = DialogMessage()
                    dialog.message = "上传失败，请重试！"
                    completeHandle(dialog)
                    return
                }
                strongSelf.tempHeadUrl = url
                sema.signal()
            }

        }
        
        // 上传头像
        queue.async { [weak self] in
            
            sema.wait()
            guard let strongSelf = self, let tempHeadUrl = strongSelf.tempHeadUrl, tempHeadUrl.count > 0 else {
                sema.signal()
                var dialog = DialogMessage()
                dialog.message = "上传失败，请重试！"
                completeHandle(dialog)
                return }
            
            MYLog("上传头像", tempHeadUrl)
            apiProvider.request(api: .uploadAvatar(params: ["url": tempHeadUrl])) { json, jsonArr, code in
                            
                MYLog("上传头像：\(json)")
                var dialog = DialogMessage()
                guard code == 200, let respone = Mapper<ResponseModel>().map(JSON: json) else {
                    sema.signal()
                    completeHandle(dialog)
                    return
                }
                kUserManager.userModel.avatar = strongSelf.tempHeadUrl!
                dialog.code = respone.code
                dialog.message = respone.msg

                sema.signal()
                completeHandle(dialog)
                
            } failureHandler: { error in
                sema.signal()
                completeHandle(DialogMessage())
            }
        }
        
    }
    
    
}
